package com.yazi.mxz.admin.controller;

import javax.annotation.Resource;

import com.yazi.mxz.common.controller.BaseController;
import com.yazi.mxz.common.entity.beans.Store;
import com.yazi.mxz.common.service.StoreService;
import com.myframework.smhj.common.tool.PageLink;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.jackson.result.StatusResult;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller("storeController")
@RequestMapping("/store")
public class StoreController extends BaseController{
	private static Logger log = LoggerFactory.getLogger(StoreController.class);
	
	@Resource
	StoreService storeService;
	
	
	@RequestMapping("/list")
	public String list(Store store, Searching si, Model model){
		log.debug("管理->进入店铺列表页面");
		Page<Store> page = storeService.list(store, si);
		PageLink pageLink = new MyPageLink(page.getPaging());
		model.addAttribute("storeList", page.getList());
		model.addAttribute("pageLink", pageLink);
		return "store_list";
	}
		
	@RequestMapping("/addOrEdit")
	public String addOrEdit(Store store, Model model){
		Long storeId = store.getStoreId();
		if(null != storeId){
			log.debug("管理->进入店铺编辑页面：storeId={}", storeId);
			store = storeService.one(storeId);
			
		}else{
			log.debug("管理->进入店铺添加页面");
			
		}
		model.addAttribute("store", store);
		return "store_addOrEdit";
	}
	
	@RequestMapping("/addOrEditDo")
	public String addOrEditDo(Store store){
		log.debug("管理->保存（添加/更新）商品");
//		storeService.addOrUpdate(store);
		return "redirect:/store/list";
	}
	
	@RequestMapping("/addMember")
	public String addMember(Long storeId, Model model){
		log.debug("店铺管理员设定页面: storeId={}", storeId);
		Store store = storeService.one(storeId);
		model.addAttribute("store", store);
		return "store_member";
	}
	
	@RequestMapping("/addMemberDo/json")
	@ResponseBody
	public StatusResult addMemberDo(Long storeId, Long[] memberIds){
		log.debug("指定店铺管理用户: storeId={},memberId={}", storeId, memberIds);
		StatusResult jsonResult = new StatusResult();
		try {
			//storeService.addMembers(storeId, memberIds);
			jsonResult.setOkMessage("提交成功");
		} catch(Exception e) {
			log.error("指定店铺管理用户异常：", e);
			jsonResult.setErrorMessage(e.getMessage());
		}
		
		return jsonResult;
	}
	
}


